package br.com.moto.web;

import br.com.moto.ejb.AlbumSessionBean;
import br.com.moto.ejb.PessoaSessionBean;
import br.com.moto.entity.Album;
import br.com.moto.entity.Pessoa;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext; 
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

@ManagedBean(name = "albumController")
@SessionScoped
public class AlbumMBean {
    
    @EJB
    private AlbumSessionBean albumSessionBean;
    
    @EJB
    private PessoaSessionBean pessoaSessionBean;
    
    private Album album = new Album();
    private Album albumSelecionado;
    
    public AlbumMBean() {
    }
 
    
    public List getAlbumToUser() { 
        return albumSessionBean.getAlbumToUser(LoginUtils.getLoggedUser());
    }     
    
    public List getListaAlbums() { 
        return albumSessionBean.retrieve();  
    }     
    
    public void adicionarAlbum() {
        System.out.println("##adicionarAlbum");
         
        
        Pessoa p = pessoaSessionBean.getPessoaByEmail(LoginUtils.getLoggedUser());
        album.setIdPessoa(p);
        albumSessionBean.create(album);   
        
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Novo album adicionado com sucesso!"));
    }
    
    public void novoAlbum() {
        System.out.println("##novoAlbum");
        album = new Album();
    }     
    
    public void carregarAlbum() {
        System.out.println("##carregarAlbum" + albumSelecionado.getId());        
        album = albumSessionBean.getAlbum(albumSelecionado);
    }    
    
    public void excluirAlbum() {
            albumSessionBean.delete(albumSelecionado);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Excluído com sucesso!"));
    }    

    public Album getAlbum() {
        return album;
    }

    public void setAlbum(Album album) {
        this.album = album;
    }

    public Album getAlbumSelecionado() {
        return albumSelecionado;
    }

    public void setAlbumSelecionado(Album albumSelecionado) {
        this.albumSelecionado = albumSelecionado;
    }
}